<?php

require_once 'Custom/Model/Db.php';

class News extends Custom_Model_Db
{
	/**
	 * The default table name 
	 */
    protected $_name 				= 	'news';
    protected $_primary 			=	'id';
    
    
	
	
	/**
	 * 后台AJAX数据列表
	 * @param String $where
	 * @param String $order
	 * @param int $count
	 * @param int $offset
	 */
	public function getAjaxBackData($where = null, $order = null, $count = null, $offset = null)
	{
		try {
           	$db = $this->getAdapter();
			$obj=$db->select()
			->from(array('a'=>$this->dbprefix.'news'),'*')
			->joinLeft(array('m'=>$this->dbprefix.'member'),'m.uid=a.uid',array("uid as comid","username"))
			->joinLeft(array('cat'=>$this->dbprefix.'category'),'a.cid=cat.id',array('name','id as catid'))
			->where($where)
			//->where("a.created <=".time())
			->order($order)
			->limit($count, $offset);
			//echo $obj->__toString();	
			//exit;
			$array=$db->fetchAll($obj);
			return $array;
        } catch (Exception $e) {
			return false;
        }
	}
    
	/**
	 * 后台AJAX数据列表
	 * @param String $where
	 * @param String $order
	 * @param int $count
	 * @param int $offset
	 */
	public function getAjaxData($where = null, $order = null, $count = null, $offset = null)
	{
		try {
           	$db = $this->getAdapter();
			$obj=$db->select()
			->from(array('a'=>$this->dbprefix.'news'),'*')
			->joinLeft(array('m'=>$this->dbprefix.'member'),'m.uid=a.uid',array("uid as comid","username"))
			->joinLeft(array('cat'=>$this->dbprefix.'category'),'a.cid=cat.id',array('name','id as catid'))
			->where($where)
			->where("a.created <=".time())
			->order($order)
			->limit($count, $offset);
			//echo $obj->__toString();	
			//exit;
			$array=$db->fetchAll($obj);
			return $array;
        } catch (Exception $e) {
			return false;
        }
	}
	
	
	/**
	 * 取到文章全部数据
	 * @param $id
	 */
	
	public function getnewsList($where = null, $order = null, $count = null, $offset = null){
		try {
			$db = $this->getAdapter();
			$obj=$db->select()
			->from(array('a'=>$this->dbprefix.'news'),'*')
			->joinLeft(array('c'=>$this->dbprefix.'news_text'),'c.aid=a.id')
			->where($where)
			->where("a.created <=".time())
			->order($order)
			->limit($count, $offset);
			
			$array=$db->fetchAll($obj);
			return $array;
		} catch (Exception $e) {
			return false;
        }
	}
	
	/**
	 * 取到文章全部数据
	 * @param $id
	 */
	
	public function getOnenews($id){
		try {
			$db = $this->getAdapter();
			$obj=$db->select()
			->from(array('a'=>$this->dbprefix.'news'),'*')
			->joinLeft(array('c'=>$this->dbprefix.'news_text'),'c.aid=a.id')
			//->where("a.created >=".time())
			->where("a.id=$id");
			$array=$db->fetchAll($obj);
			return $array;
			
		} catch (Exception $e) {
			return false;
        }
	}
	
	
	public function getViewData($id) {
		try {
           	$db = $this->getAdapter();
			$select=$db->select()
			->from(array('a'=>$this->dbprefix.'news'),'*')
			->joinLeft(array('t'=>$this->dbprefix.'news_text'),'t.aid=a.id')
			->joinLeft(array('c'=>$this->dbprefix.'company'),'a.uid=c.uid',array('company','uid'))
			->where("a.created <=".time())
			->where("a.id=$id AND a.status=1 ");
			$sql = $select->__toString();//生成SQL语句
			$array = $db->fetchAll($sql);
        	return $array;
        } catch (Exception $e) {
			return false;
        }
	}
	

	
   
	
    
	
    
}
	
